package com.hjs.mr.merge_and_sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 这个是读取数据之后整合封装的实体Bean，继承WritableComparable接口，用于给数字从小打到排序
 */
public class SortBean implements WritableComparable<SortBean> {

    //定义成员变量，一个是读取到的数字内容,另一个是数字的排序
    private int number;
    private int sort;

    //有参构造
    public SortBean(int number) {
        this.number = number;
        this.sort = 0;
    }

    //无参构造
    public SortBean() {
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    //定义排序规则
    @Override
    public int compareTo(SortBean sortBean) {
        return Integer.compare(this.number, sortBean.number);
    }

    //将数据写入Bean
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(number);
        dataOutput.writeInt(sort);
    }

    //从Bean中读取数据
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.number = dataInput.readInt();
        this.sort = dataInput.readInt();
    }

    //重写toString方法，为了最后写入到输出文件中去
    @Override
    public String toString() {
        return sort + "\t" + number;
    }
}
